/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */
package com.BCNTours.Vista;

import java.util.Vector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ParseException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.BCNTours.R;
import com.BCNTours.Model.ApplicationData;
import com.BCNTours.Vista.Preferences;
import com.BCNTours.Vista.RowData;

/**
 * Displays the list with the different categories of POI.
 *
 */
public class Categories extends ListActivity {
	
	/*Attributions*/
	private Vector<RowData> data;
	private RowData rd;
	private CustomAdapter adapter;	
	private ApplicationData applicationData;
	
	String[] typeEsp = new String[] {
			"Atracciones",
			"Monumentos",
			"Museos",
			"Puntos de Información",
			"Todos"
	};
	
	String[] typeCat = new String[] {
			"Atraccions",
			"Monuments",
			"Museus",
			"Punts d'Informació",
			"Tots"
	};
	
	String[] typeEng = new String[] {
			"Attractions",
			"Monuments",
			"Museums",
			"Information points",
			"All"
	};
	
	Integer[] imgid = new Integer[]{ 
			R.drawable.iconoatracciones, R.drawable.iconomonumentos, R.drawable.iconomuseos, R.drawable.iconoinfo, R.drawable.iconoall
	}; 
	
	/** 
	 * Called when the activity is first created. 
	 * */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categories);
        
        //Animation of the screen
        LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        
        //Get all extras previously added with putExtra()
    	Bundle extras = getIntent().getExtras();
        applicationData = (ApplicationData) extras.getSerializable("com.ShowMe.Model.PoiList");
         
        // Upload rowdata with category types
        data = new Vector<RowData>();
        String[] typeAux = new String[]{};
        
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        int lang= prefs.getInt(Preferences.PREF_LANG, 3); 
        
        switch (lang) {
		case 1:
			typeAux = typeEsp;
			break;
		case 2:
			typeAux = typeCat;
			break;
		case 3:
			typeAux = typeEng;
			break;
		}
	
        for(int i = 0; i < typeAux.length; i++){
        	try {
    			rd = new RowData(i, typeAux[i], imgid[i]);
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
             data.add(rd);
        }
        
        //Add the rowdata to the layout
        adapter = new CustomAdapter(this, mInflater ,R.layout.favorites_list, R.id.title, data);
        setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);  
      }
    
    /**
     * Create the menu of the intent
     */
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menufavorits, menu);
		return true;
    }
    
	/**
	 * Action listener of the search button
	 */
	@Override
	public boolean onSearchRequested() {
		Intent intent = new Intent(getBaseContext(), Search.class);
    	intent.putExtra("com.ShowMe.Model.PoiList", applicationData);
    	intent.putExtra("tipo", 4);
    	startActivity(intent);
		setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
		return super.onSearchRequested();
	}
    
    /**
     * Action listener of list
     */
    public void onListItemClick(ListView parent, View v, int position, long id) { 
    	
    	//Change the intent
    	Intent intent = new Intent(getBaseContext(), CategoryList.class);
    	intent.putExtra("com.ShowMe.Model.PoiList", applicationData);
    	intent.putExtra("tipo", position);
    	startActivity(intent);
		setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
    }
    
    /**
     * Action listener of the menu
     */
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()){
    		case R.id.menuSearch:
    			
    			Intent intent = new Intent(getBaseContext(), Search.class);
    	    	intent.putExtra("com.ShowMe.Model.PoiList", applicationData);
    	    	intent.putExtra("tipo", 4);
    	    	startActivity(intent);
    			setListAdapter(adapter);
    	        getListView().setTextFilterEnabled(true);
    			break;
    	}
    	return false;
    }
}